<template>
	<view v-if="appType === 'app-tch' || appType === 'mp-tch'">
		<plugin></plugin>
	</view>
	<view v-else-if="(appType === 'app-p' || appType === 'mp-p') && channel !== 'xm'" class="container">
		<view class="plugin-parent">
			<u-grid :col="4" :border="false">
				<u-grid-item v-for="(item, index) in list" :key="index" @click="itemClick(item)">
					<u-image width="120" height="165" :src="item.icon"></u-image>
					<view>{{ item.title }}</view>
				</u-grid-item>
			</u-grid>
		</view>
		<feed ref="feed" :isCourse="false"></feed>
	</view>
	<view v-else>

	</view>
</template>

<script>
	import {
		getTicket
	} from '@/utils/auth.js'
	import Plugin from '@/pages/plugin.vue'
	import Feed from '@/pages/feed/feed.vue'
	import checkUpdate from '@/uni_modules/uni-upgrade-center-app/utils/check-update.js'
	import {
		getAppEdition,
		getAppEditionByParent
	} from "@/api/common.js"
	import {
		getBabylist,
	} from '@/api/user.js'
	import {
		mapState,
		mapGetters
	} from 'vuex';
	import appMixin from '@/mixins/app.js'
	export default {
		mixins: [appMixin],
		components: {
			Plugin,
			Feed,
		},
		computed: {
			...mapState({
				appType: state => state.app.appType,
				parentInfo: state => state.user.parentInfo,
			}),
			channel() {
				return getApp().globalData.channel;
			},
		},
		data() {
			return {
				appId: '',
				appVersion: '',
				wgtVersion: '',
				list: [{
						title: '宝宝测评',
						url: '/pages_evaluate/evaluate/evl-record',
						icon: 'https://dotflat-saas-res.oss-cn-shanghai.aliyuncs.com/xybaby365/app/icons/plugin_parent_v2_1.png'
					},
					{
						title: '时光轴',
						url: '/pages_base/feed/timeline',
						icon: 'https://dotflat-saas-res.oss-cn-shanghai.aliyuncs.com/xybaby365/app/icons/plugin_parent_v2_2.png'
					},
					{
						title: '考勤记录',
						url: '/pages_sub/check/attendance',
						icon: 'https://dotflat-saas-res.oss-cn-shanghai.aliyuncs.com/xybaby365/app/icons/plugin_parent_v2_3.png'
					},
					{
						title: '一日教保',
						url: '/pages_sub/record/daily-children-page-new',
						icon: 'https://dotflat-saas-res.oss-cn-shanghai.aliyuncs.com/xybaby365/app/icons/plugin_parent_v2_4.png'
					},
					{
						title: '周食谱',
						url: '/pages_sub/recipe/recipe-page',
						icon: 'https://dotflat-saas-res.oss-cn-shanghai.aliyuncs.com/xybaby365/app/icons/plugin_parent_v2_5.png'
					},
					{
						title: '托药',
						url: '/pages_sub/record/handover-page',
						icon: 'https://dotflat-saas-res.oss-cn-shanghai.aliyuncs.com/xybaby365/app/icons/plugin_parent_v2_6.png'
					},
					{
						title: '喂养',
						url: '/pages_sub/record/feed-list-page',
						icon: 'https://dotflat-saas-res.oss-cn-shanghai.aliyuncs.com/xybaby365/app/icons/plugin_parent_v2_7.png'
					},
					// {
					// 	title: '疫苗信息',
					// 	url: '/pages_add/vaccine/vaccine-page',
					// 	icon: 'https://dotflat-saas-res.oss-cn-shanghai.aliyuncs.com/xybaby365/app/icons/plugin_parent_6.png'
					// },
					// {
					// 	title: '请假申请',
					// 	url: '/pages_add/leave/leave-page',
					// 	icon: 'https://dotflat-saas-res.oss-cn-shanghai.aliyuncs.com/xybaby365/app/icons/plugin_parent_6.png'
					// },
					// {
					// 	title: '预约到访',
					// 	url: '/pages_add/visit/visit-page',
					// 	icon: 'https://dotflat-saas-res.oss-cn-shanghai.aliyuncs.com/xybaby365/app/icons/plugin_parent_6.png'
					// },
					// {
					// 	title: '预约试托',
					// 	url: '/pages_add/childcare/childcare-page',
					// 	icon: 'https://dotflat-saas-res.oss-cn-shanghai.aliyuncs.com/xybaby365/app/icons/plugin_parent_6.png'
					// },
					// {
					// 	title: '营养食谱',
					// 	url: '/pages_sub/recipe/recipe-month-page',
					// 	icon: 'https://dotflat-saas-res.oss-cn-shanghai.aliyuncs.com/xybaby365/app/icons/plugin_parent_6.png'
					// },
				],
			}
		},
		onUnload() {
			// #ifdef APP-PLUS
			plus.screen.lockOrientation('portrait-primary');
			// #endif
		},
		onLoad() {
			// #ifdef APP-PLUS
			plus.screen.lockOrientation('portrait-primary');
			// #endif

			// #ifndef H5
			const ticket = getTicket();
			if (this.$u.test.isEmpty(ticket)) {
				uni.redirectTo({
					url: '/pages/login',
				})
				return;
			};
			// #endif
			// #ifdef H5
			if (process.env.NODE_ENV === 'development') {
				const ticket = getTicket();
				if (this.$u.test.isEmpty(ticket)) {
					uni.redirectTo({
						url: '/pages/login',
					})
					return;
				};
			}
			// #endif
			this.titleDndTabberSet()
		},
		mounted() {
			this.titleDndTabberSet()
		},
		methods: {
			titleDndTabberSet() {
				if (this.appType === 'app-tch' || this.appType === 'mp-tch') {
					this.$store.dispatch('user/refreshTeacherInfo', {})
					uni.showTabBar();
					uni.setNavigationBarTitle({
						title: '园区'
					})
					uni.setTabBarItem({
						index: 0,
						text: '园区',
						visible: true
					})
					uni.setTabBarItem({
						index: 1,
						text: '班级圈',
						visible: true
					})
					uni.setTabBarItem({
						index: 2,
						text: '活动通知',
						visible: true
					})
					
					uni.setTabBarItem({
						index: 3,
						text: '我的',
						visible: true
					})
					// #ifdef APP-PLUS
					this.checkAppUpdate()
					// #endif
				} else if (this.appType === 'app-p' || this.appType === 'mp-p') {
					this.$store.dispatch('user/refreshParentInfo', {})
					if (this.channel === 'xm') {
						uni.switchTab({
							url: '/pages/tabbar-item3',
							fail: res => {
								console.log(res)
								uni.showModal({
									title: '启动失败,请重新登录!',
									content: `失败原因:${res}`,
									showCancel: false,
									// success: res => {
									// 	this.$store.commit('user/logout');
									// 	uni.reLaunch({
									// 		url: '/pages/login'
									// 	})
									// }
								})
							}
						})
					} else {
						// #ifdef APP-PLUS
						this.checkAppUpdate()
						// #endif
					}
					let tabBar = '在园'
					if (this.channel === 'xxwbaby') {
						tabBar = '在托'
					}
					uni.setNavigationBarTitle({
						title: tabBar
					})
					uni.setTabBarItem({
						index: 0,
						text: tabBar,
					});
					uni.setTabBarItem({
						index: 1,
						text: '在家',
					});
					uni.setTabBarItem({
						index: 2,
						text: '活动通知',
					})
					uni.setTabBarItem({
						index: 3,
						text: '我的',
					})
				}
			},
			itemClick(item) {
				this.$u.route({
					url: item.url
				})
			},

			checkAppUpdate() {
				let appType = '1'
				if (uni.getSystemInfoSync().platform === 'android') {
					const main = plus.android.runtimeMainActivity();
					const pkName = main.getPackageName();
					this.appId = pkName
				} else {
					const packageName = plus.ios.importClass('NSBundle').mainBundle().bundleIdentifier()
					this.appId = packageName
					appType = '2'
				}

				plus.runtime.getProperty(plus.runtime.appid, (widgetInfo) => {
					console.log(widgetInfo)
					this.appVersion = plus.runtime.version
					this.wgtVersion = widgetInfo.version

					this.getAppPackage(this.appId, this.appVersion, this.wgtVersion, appType)
				})
			},

			getAppPackage(appId, appVersion, wgtVersion, appType) {
				let getAppVersion = getAppEditionByParent
				if (this.appType === 'app-tch' || this.appType === 'mp-tch') {
					getAppVersion = getAppEdition
				}

				getAppVersion({
					appId,
					appVersion,
					wgtVersion,
					type: appType
				}).then(res => {
					console.log(res)
					if (res.data) {
						const verionInfo = res.data
						if (res.data.wrapType === 'wgt') {
							uni.downloadFile({
								url: res.data.url,
								success: res => {
									if (res.statusCode == 200) {
										// 下载好直接安装，下次启动生效
										plus.runtime.install(res.tempFilePath, {
											force: false
										});
									}
								}
							});
						} else {
							const PACKAGE_INFO_KEY = '__package_info__'
							const data = {
								appid: appId,
								code: 102,
								contents: verionInfo.content,
								create_date: 1669209718191,
								create_env: "upgrade-center",
								is_mandatory: verionInfo.isMandatory === 1 ? true : false,
								is_silently: false,
								message: "整包更新",
								name: verionInfo.name,
								platform: verionInfo.type === 2 ? ["iOS"] : ["Android"],
								stable_publish: true,
								title: verionInfo.title,
								type: "native_app",
								uni_platform: verionInfo.type === 2 ? "ios" : 'android',
								url: verionInfo.type === 2 ? verionInfo.storeUrl : verionInfo.url,
								version: verionInfo.version,
								_id: "637e1e76819ce86622fefb85"
							}
							console.log(data)
							uni.setStorageSync(PACKAGE_INFO_KEY, data)

							uni.navigateTo({
								url: `/uni_modules/uni-upgrade-center-app/pages/upgrade-popup?local_storage_key=${PACKAGE_INFO_KEY}`,
								fail: (err) => {
									console.error('更新弹框跳转失败', err)
									uni.removeStorageSync(PACKAGE_INFO_KEY)
								}
							})
						}
					}

				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		background-color: $app-page-bg-color;
	}

	.container {
		display: flex;
		flex-direction: column;
		.title-name{
			font-size: 32rpx;
			font-weight: bold;
			color: #09153D;
			text-align: center;
			padding-bottom: 58rpx;
		}
		.plugin-parent {
			border-radius: 20rpx;
			overflow: hidden;
			background-color: #FFFFFF;
		}
	}

	.title {
		font-size: 24rpx;
		font-family: PingFang SC;
		font-weight: 400;
		color: #333333;
		margin-top: 20rpx;
	}
</style>